extends Camera2D

@export var zoomSpeed: float = 10
@export var panSpeed: float = 400
var zoomTarget: Vector2

var dragStart := Vector2.ZERO

func _ready():
	zoomTarget = zoom

func _input(event):
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_MIDDLE and event.pressed:
		dragStart = get_global_mouse_position()
		
func _physics_process(delta):
	zoomEvent(delta)
	panEvent(delta)
	dragEvent()

func zoomEvent(delta):
	if Input.is_action_just_pressed('zoomIn'):
		zoomTarget *= 1.1
	if Input.is_action_just_pressed('zoomOut'):
		zoomTarget *= 0.9

	zoom = zoom.slerp(zoomTarget, zoomSpeed * delta)

func panEvent(delta):
	var direction = Vector2.ZERO
	if Input.is_action_pressed('cameraDown'):
		direction.y += 1
	if Input.is_action_pressed('cameraUp'):
		direction.y -= 1
	if Input.is_action_pressed('cameraLeft'):
		direction.x -= 1
	if Input.is_action_pressed('cameraRight'):
		direction.x += 1

	var num = direction.normalized() * delta * panSpeed / zoom.x
	position += num

func dragEvent():
	if Input.is_mouse_button_pressed(MOUSE_BUTTON_MIDDLE):
		var movement = dragStart - get_global_mouse_position()
		position += movement
